package com.htfyun.fragment.toc;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.htfyun.fragment.listener.OnRecyclerViewItemClickListener;
import com.htfyun.fragment.toc.adapter.TOCAdapter;
import com.htfyun.view.EmptyRecyclerView;

import org.geometerplus.fbreader.bookmodel.TOCTree;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.ui.android.R;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by sst on 2017/11/3.
 */

public class TocFragment extends SupportFragment {

    private EmptyRecyclerView mRecy;

    private TOCAdapter mAdapter;

    List<TOCTree> tocTreeList = new ArrayList<>();


    public static TocFragment newInstance() {

        Bundle args = new Bundle();

        TocFragment fragment = new TocFragment();
        fragment.setArguments(args);
        return fragment;
    }
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.htfyun_toc_fragment, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mRecy = (EmptyRecyclerView) view.findViewById(R.id.recyclerView);

        View emptyView = view.findViewById(R.id.empty_view);

        mRecy.setEmptyView(emptyView);

    }
    
    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        mRecy.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecy.setHasFixedSize(true);
        final int space = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.5f, getResources().getDisplayMetrics());
        mRecy.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(0, 0, 0, space);
            }
        });
        
        mAdapter = new TOCAdapter(_mActivity);
        mRecy.setAdapter(mAdapter);

        final FBReaderApp fbreader = (FBReaderApp) ZLApplication.Instance();
        for (TOCTree tree : fbreader.Model.TOCTree) {
            if (android.text.TextUtils.isEmpty(tree.getText())) {
                continue;
                
            }
            tocTreeList.add(tree);
        }
        
        mAdapter.setDatas(tocTreeList);
        
        TOCTree treeToSelect = fbreader.getCurrentTOCElement();
        mAdapter.setSelectedTOCTree(treeToSelect);

        mRecy.scrollToPosition(tocTreeList.indexOf(treeToSelect) - 1);
        
        mAdapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(int position, View view, RecyclerView.ViewHolder vh) {

                TOCTree tree = tocTreeList.get(position);
                openBookText(tree);
                
            }
        });

    }

    void openBookText(TOCTree tree) {
        final TOCTree.Reference reference = tree.getReference();
        if (reference != null) {
            getActivity().finish();
            final FBReaderApp fbreader = (FBReaderApp)ZLApplication.Instance();
            fbreader.addInvisibleBookmark();
            fbreader.BookTextView.gotoPosition(reference.ParagraphIndex, 0, 0);
            fbreader.showBookTextView();
            fbreader.storePosition();
        }
    }
}
